$(document).ready(function() {
    var queryString = location.search;
    var parameters = Request.parseQueryString(queryString);

    var queryTime = null;
    var queryStartTime = null;
    var queryEndTime = null;
    var grade = parameters.grade;
    var natureClass = parameters.nature_class;
    var device = parameters.device;

    if ( parameters.time ) {
        queryTime = new Date(Number(parameters.time));
    }

    if ( parameters.start && parameters.end ) {
        queryStartTime = new Date(Number(parameters.start));
        queryStartTime.setMinutes(queryStartTime.getMinutes() + 1);
        queryEndTime = new Date(Number(parameters.end));
    }

    var $queryTime = $('.input-query-time').datepicker({
    });
    $queryTime.datepicker('update', new Date());
    if ( queryTime ) {
        $queryTime.datepicker('update', queryTime);
    }
    
    var $queryStartTime = $('.input-start-time').datepicker({
    });
    $queryStartTime.datepicker('update', new Date());
    if ( queryStartTime ) {
        $queryStartTime.datepicker('update', queryStartTime);
    }

    var $queryEndTime = $('.input-end-time').datepicker({
    });
    $queryEndTime.datepicker('update', new Date());
    if ( queryEndTime ) {
        $queryEndTime.datepicker('update', queryEndTime);
    }

    var $gradeSelect = $('.select-grade');
    if ( grade ) {
        $gradeSelect.val(grade);
        var gradeName = $('.menu-grades a[value="' + grade + '"]').text();
        $gradeSelect.parent('a').find('span').text(gradeName);
    }

    var $natureClassSelect = $('.select-class');
    if ( natureClass ) {
        $natureClassSelect.val(natureClass);
        var natureClassName = $('.menu-classes a[value="' + natureClass + '"]').text();
        $natureClassSelect.parent('a').find('span').text(natureClassName);
    }

    var $deviceSelect = new DropdownSelect('.select-device');
    if ( device ) {
        $deviceSelect.value = device;
    }

    $('.btn-search').click(function(e) {
        disableAll(e);
    
        var time = $queryTime.datepicker('getDate');

        var startTime = new Date(time.getFullYear(), time.getMonth(), time.getDate());
        var endTime = new Date(time.getFullYear(), time.getMonth(), time.getDate());

        startTime.setMinutes(startTime.getMinutes() - 1);
        endTime.setDate(endTime.getDate() + 1);
        endTime.setMinutes(endTime.getMinutes() - 1);

        // 必填参数
        var queryCondition = {
            time: time.getTime(),
            start: startTime.getTime(),
            end: endTime.getTime()
        };

        var natureClass = $natureClassSelect.val();
        if ( natureClass != '0' ) {
            queryCondition.nature_class = natureClass;
        }

        console.log(queryCondition);
        request = new Request('search', queryCondition);

        response = new Response();
        response.redirectTo(request);
    });

    var type = parameters.type;
    var $rawAttendanceTypeSelect = $('.select-type');
    if ( 'type' in parameters ) {
        $rawAttendanceTypeSelect.val(type);
        var typeName = $('.menu-types a[value="' + type + '"]').text();
        $rawAttendanceTypeSelect.parent('a').find('span').text(typeName);
    }

    $('.btn-search-raw-attendances').click(function(e) {
        disableAll(e);
    
        var startTime = $queryStartTime.datepicker('getDate');
        startTime = new Date(startTime.getFullYear(), startTime.getMonth(), startTime.getDate());
        var endTime = $queryEndTime.datepicker('getDate');
        endTime = new Date(endTime.getFullYear(), endTime.getMonth(), endTime.getDate());
    
        startTime.setMinutes(startTime.getMinutes() - 1);
        endTime.setDate(endTime.getDate() + 1);
        endTime.setMinutes(endTime.getMinutes() - 1);

        // 必填参数
        var queryCondition = {
            start: startTime.getTime(),
            end: endTime.getTime()
        };

        // 可选参数
        var type = $rawAttendanceTypeSelect.val();
        if ( type != '-1' ) {
            queryCondition.type = type;
        }
        
        var device = $deviceSelect.value;
        if ( device ) {
            queryCondition.device = device;
        }

        console.log(queryCondition);
        if (!queryCondition.start || !queryCondition.end) {
            alert('时间不能为空！');
            return;
        }
        if (queryCondition.start > queryCondition.end) {
            alert('开始时间大于结束时间！');
            return;
        }
        request = new Request('/leader/device/manage', queryCondition);

        response = new Response();
        response.redirectTo(request);
    });
});
